package lrh.hubu.firstproject;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class talkAdapter extends RecyclerView.Adapter<talkAdapter.talkViewHolder> implements ItemTouchHelperListener {
    private Context mContext;
    private View view;

    public static final int FIRST_STICKY_VIEW = 1;
    public static final int HAS_STICKY_VIEW = 2;
    public static final int NONE_STICKY_VIEW = 3;

    private ArrayList<mydata> mList = new ArrayList<mydata>();

    public talkAdapter(Context context){
        mContext = context;
    }

    public void setDragDataList(ArrayList<mydata> list) {
        mList = list;

        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public talkViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        view = LayoutInflater.from(mContext).inflate(R.layout.item_3, parent, false);
        return new talkViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull talkViewHolder holder, int position) {
//        holder.textView.setText(mList.get(position));

        final String content = mList.get(position).getName();
        mydata stickyData = mList.get(position);

        holder.textView.setText(stickyData.getName());

        if (position == 0) {
            holder.t1.setVisibility(View.VISIBLE);
        holder.t1.setText(stickyData.group);
        holder.itemView.setTag(FIRST_STICKY_VIEW);
    } else {
        if (!TextUtils.equals(stickyData.group, mList.get(position - 1).group)) {
            holder.t1.setVisibility(View.VISIBLE);
            holder.t1.setText(stickyData.group);
            holder.itemView.setTag(HAS_STICKY_VIEW);
        } else {
            holder.t1.setVisibility(View.GONE);
            holder.itemView.setTag(NONE_STICKY_VIEW);
        }
    }
        holder.itemView.setContentDescription(stickyData.group);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, "你点击的是：" + content, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    @Override
    public void onItemMove(int fromPosition, int toPosition) {
        String prev = mList.remove(fromPosition).getName();
        mList.add(toPosition > fromPosition ? toPosition - 1 : toPosition, new mydata(mList.remove(fromPosition).getGroup(),prev));
        notifyItemMoved(fromPosition, toPosition);
    }

    public void onItemDismiss(int position) {
        if (position < 0 || position > getItemCount()) {
            return;
        }

        mList.remove(position);
        notifyItemRemoved(position);

        // 解决 RecyclerView 删除 Item 导致位置错乱的问题
        if (position != mList.size()) {
            notifyItemRangeChanged(position, mList.size() - position);
        }
    }

    public class talkViewHolder extends RecyclerView.ViewHolder {
        TextView textView,t1;
        public talkViewHolder(@NonNull View itemView) {
            super(itemView);
            t1 = itemView.findViewById(R.id.t3_top);
            textView = itemView.findViewById(R.id.t3_1);
        }
    }
}
